package com.dao;

import com.model.StuException;
import com.model.Student;
import com.model.Teacher;
import com.util.XmlUtil;
import org.dom4j.Document;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * Created by guin_guo on 2015/10/27.
 * 完成对教师对象的增删改查
 */
public class TeacherDao {
    private static final String TEAC_XML="teachers.xml";
    private Document sd;

    private TeacherDao() {
        sd = XmlUtil.getTeacherDocument();
    }
    private static class Singleton{
        public static TeacherDao instance = new TeacherDao();
    }

    /**
     * 使用TeacherDao.getInstance()获取Dao的单例
     * @return
     */
    public static TeacherDao getInstance(){
        return (Singleton.instance);
    }
    private void write(){
        XmlUtil.write2XML(sd,TEAC_XML);
    }
    public void add(Teacher teacher) {
        if("".equals(teacher.getAccount())||teacher.getAccount()==null){
            throw new StuException("用户名不能为空");
        }
        Teacher s = this.load(teacher.getAccount());
        if(s!=null) {
            throw new StuException("教师信息已存在，不能继续添加");
        }
        Element es = sd.getRootElement().addElement("teacher");
        es.addAttribute("id", String.valueOf(XmlUtil.getMaxId(sd,"/teachers/teacher")+1));
        es.addElement("account").addText(teacher.getAccount());
        es.addElement("name").addText(teacher.getName());
        es.addElement("sex").addText(teacher.getSex());
        es.addElement("birth").addText(teacher.getBirth());
        es.addElement("password").addText(teacher.getPassword());
        es.addElement("age").addText(teacher.getAge());
        es.addElement("place").addText(teacher.getPlace());
        es.addElement("status").addText(teacher.getStatus());
        es.addElement("major").addText(teacher.getMajor());
        es.addElement("college").addText(teacher.getCollege());
        es.addElement("workYear").addText(teacher.getWorkYear());
        es.addElement("job").addText(teacher.getJob());
        write();
    }
    public void delete(String name) {
        Element root = sd.getRootElement();
        Element s = loadElementByAccount(name);
        if(s==null) {
            throw new StuException("要删除的教师不存在");
        }
        root.remove(s);
        write();
    }
    public void update(Teacher teacher) {
        Element e = loadElementByAccount(teacher.getAccount());
        if (e == null) {
            throw new StuException("要更新的学生不存在");
        }
        e.element("name").setText(teacher.getName());
        e.element("sex").setText(teacher.getSex());
        e.element("place").setText(teacher.getPlace());
        e.element("birth").setText(teacher.getBirth());
        e.element("age").setText(teacher.getAge());
        e.element("status").setText(teacher.getStatus());
        e.element("college").setText(teacher.getCollege());
        e.element("major").setText(teacher.getMajor());
        e.element("password").setText(teacher.getPassword());
        e.element("workYear").setText(teacher.getWorkYear());
        e.element("job").setText(teacher.getJob());

        write();
    }

    public Teacher load(String account) {
        Element e = loadElementByAccount(account);
        if (e == null) {
            return null;
        }
        Teacher teacher = eLement2Stu(e);
        return teacher;
    }
	/**
	*通过老师姓名查找老师
	*/
	public Teacher findByName(String name) {
		String path = "/teachers/teacher[name='"+name+"']";
        Element e = (Element)sd.selectSingleNode(path);
        if (e == null) {
            return null;
        }
        Teacher teacher = eLement2Stu(e);
        return teacher;
    }
    public List<Teacher> list(){
        String path = "/teachers/teacher";
        List<Element> eles = sd.selectNodes(path);
        List<Teacher> teacher = new ArrayList<Teacher>(eles.size());
        for (Element e : eles) {
            teacher.add(eLement2Stu(e));
        }
        return teacher;
    }
    public List<Vector> vectorList(){
        List<Teacher> teachers = this.list();
        List<Vector> list = new ArrayList<>(teachers.size());
        for (Teacher t: teachers) {
            Vector<String> vector = new Vector<>();
            vector.add(t.getAccount());
            vector.add(t.getName());
            vector.add(t.getAge());
            vector.add(t.getSex());
            vector.add(t.getBirth());
            vector.add(t.getPlace());
            vector.add(t.getStatus());
            vector.add(t.getCollege());
            vector.add(t.getMajor());
            vector.add(t.getWorkYear());
            vector.add(t.getJob());
            list.add(vector);
        }
        return list;
    }
    public Teacher login(String account,String password) {
        Teacher teacher = load(account);
        if(teacher==null) throw new StuException("用户名不正确!");
        if(!teacher.getPassword().equals(password)) throw new StuException("密码不正确!");
        return teacher;
    }
    private Teacher eLement2Stu(Element e) {
        Teacher teacher = new Teacher();
        teacher.setId(Integer.parseInt(e.attributeValue("id")));
        teacher.setAccount(e.elementText("account"));
        teacher.setPassword(e.elementText("password"));
        teacher.setBirth(e.elementText("birth"));
        teacher.setCollege(e.elementText("college"));
        teacher.setName(e.elementText("name"));
        teacher.setStatus(e.elementText("status"));
        teacher.setMajor(e.elementText("major"));
        teacher.setPlace(e.elementText("place"));
        teacher.setSex(e.elementText("sex"));
        teacher.setAge(e.elementText("age"));
        teacher.setWorkYear(e.elementText("workYear"));
        teacher.setJob(e.elementText("job"));
        return teacher;
    }
    private Element loadElementByAccount(String account){
        String path = "/teachers/teacher[account='"+account+"']";
        Element e = (Element)sd.selectSingleNode(path);
        return e;
    }


    public List<String> findByAccount(String account){
        Teacher t = this.load(account);
        if(t==null){
            return null;
        }
        List<String> list = new ArrayList<>();
        list.add(t.getAccount());
        list.add(t.getName());
        list.add(t.getAge());
        list.add(t.getSex());
        list.add(t.getBirth());
        list.add(t.getPlace());
        list.add(t.getStatus());
        list.add(t.getCollege());
        list.add(t.getMajor());
        list.add(t.getWorkYear());
        list.add(t.getJob());
        return list;
    }

    public String[] getTList() {
        String[] teacher = new String[list().size()];
        for (int i = 0; i < list().size(); i++) {
            teacher[i] = list().get(i).getName();
        }
        return teacher;
    }

}
